package org.ilead.iserve.service.manager;

import java.util.List;
import java.util.Map;

import org.ilead.iserve.constants.SessionKeyConstant;
import org.ilead.iserve.model.PrivateMessage;
import org.ilead.iserve.model.SystemMessage;
import org.ilead.iserve.model.User;
import org.ilead.iserve.model.UserSystemMessageAssociate;
import org.ilead.iserve.service.IPrivateMessageService;
import org.ilead.iserve.service.ISystemMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;

public class MessageManager {
	private static Logger logger = LoggerFactory.getLogger(MessageManager.class);
	private static MessageManager instance = new MessageManager();
	private ISystemMessageService systemMessageService;
	private IPrivateMessageService privateMessageService;
	
	/**
	 * 缓存当前分页的私信对象列表
	 */
	private List<PrivateMessage> privateMessageList;
	
	public static MessageManager getInstance(){
		return instance;
	}
	
	private MessageManager(){
		
	}
	
	public void setSystemMessageService(
			ISystemMessageService systemMessageService) {
		this.systemMessageService = systemMessageService;
	}

	public void setPrivateMessageService(
			IPrivateMessageService privateMessageService) {
		this.privateMessageService = privateMessageService;
	}

	/**
	 * 根据用户Id用户信件的长度(包括系统信和私信)
	 * @param userId
	 * @return
	 */
	public int getMessageLength(int userId){
		int systemCount = systemMessageService.findUnReadCountByUserId(userId);
		int privateCount = privateMessageService.findUnReadCountByUserId(userId);
		systemCount = systemCount >= 0 ? systemCount : 0;
		privateCount = privateCount >= 0 ? privateCount : 0;
		return systemCount + privateCount;
	}
	
	/**
	 * 更新信件的数量
	 */
	public void updateMessageCountSession(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		if(session != null){
			User user = (User)session.get(SessionKeyConstant.LOGIN_USER);
			if(user != null){
				session.put(SessionKeyConstant.MESSAGE_COUNT, MessageManager.getInstance().getMessageLength(user.getId()));
			}
		}
	}
	
	public void addSystemMessage(SystemMessage message){
		systemMessageService.addMessage(message);
		updateMessageCountSession();
	}
	
	public void addPrivateMessage(PrivateMessage message){
		privateMessageService.addMessage(message);
	}
	
	public void deletePrivateMessage(int messageId){
		privateMessageService.deleteMessageById(messageId);
	}

	public List<PrivateMessage> getPrivateMessageList() {
		return privateMessageList;
	}

	public void setPrivateMessageList(List<PrivateMessage> privateMessageList) {
		this.privateMessageList = privateMessageList;
	}
	
	/**
	 * 根据MessageId获取私信对象
	 * @param messageId
	 * @return
	 */
	public PrivateMessage getPrivateMessageById(int messageId){
		if(privateMessageList != null && privateMessageList.size() > 0){
			for (PrivateMessage message : privateMessageList){
				if(message.getId().intValue() == messageId)
					return message;
			}
		}
		return privateMessageService.findById(messageId);
	}
	
	public void updatePrivateMessage(PrivateMessage msg){
		privateMessageService.updateMessage(msg);
	}
	
	/**
	 * 将显示的当前页私信列表全部标为已读
	 */
	public void readListPrivateMessage(){
		if(privateMessageList != null && privateMessageList.size() > 0){
			for (PrivateMessage message : privateMessageList){
				if(message.getReState().intValue() == 0){
					message.setReState(1);
					updatePrivateMessage(message);
				}
			}
		}
		updateMessageCountSession();
	}
	
	/**
	 * 将显示的当前页私信列表全部标为已读
	 */
	public void readListSystemMessage(List<SystemMessage> systemMessageList){
		int userId = UserManager.getInstance().getLoginUser().getId();
		if(systemMessageList != null && systemMessageList.size() > 0){
			for (SystemMessage message : systemMessageList){
				UserSystemMessageAssociate asso = systemMessageService.findByUserIdAndMessageId(userId, message.getId());
				if(asso == null){
					asso = new UserSystemMessageAssociate(userId, message.getId(), 1);
					systemMessageService.saveOrUpdateMessage(asso);
				}else if(asso.getState().intValue() != 1){
					asso.setState(1);
					systemMessageService.saveOrUpdateMessage(asso);
				}
			}
		}
		updateMessageCountSession();
	}
	
}
